import axios from 'axios'
import qs from 'qs'

import { baseURL } from '../config'
console.log(baseURL)
const AxiosInstance = axios.create({
    baseURL: baseURL, // api的base_url
    transformRequest: [function (data) {
        data = qs.stringify(data)
        return data
    }],
    headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Device': 'mobile' }
})

AxiosInstance.interceptors.request.use(config => {
    // if (store.getters.token) {
    //     config.headers['Authorization'] = 'Bearer ' + store.getters.token
    // }
    return config
}, error => {
    return Promise.reject(error)
})

AxiosInstance.interceptors.response.use(response => {
    let data = response.data
    if (data.success) {
        return data.data
    } else {
        console.log(data.err_msg)
    }
}, error => {
    return Promise.reject(error)
})

export default function ({ type, url, data }) {
    let options = {
        url: url,
        method: type,
        [type.toUpperCase() === 'GET' ? 'params' : 'data']: data
    }

    return AxiosInstance(options)
}
